#include <iostream>
#include <string.h>
#include "Computer"

using std::cout;
using std::endl;

Computer *Computer::_pInstance = nullptr;

Computer * Computer::getInstance()
{
    if(_pInstance == nullptr)
    {
        _pInstance = new Computer(0, "NULL");
    }

    return _pInstance;
}

Computer * Computer::getInstance(int price, const char *brand)
{
    if(_pInstance == nullptr)
    {
        _pInstance = new Computer(price, brand);
    }

    return _pInstance;
}

void Computer::init(int price, const char *brand)
{
    if(_pInstance){
        delete [](_pInstance->_brand);
        _pInstance->_brand = nullptr;
    }
    this->_price = price;
    this->_brand = new char[strlen(brand) + 1];
    strcpy(_brand, brand);

    return;
}

void Computer::destroy()
{
    if(_pInstance)
    {
        delete [](_pInstance->_brand);
        delete _pInstance;
        _pInstance->_brand = nullptr;
        _pInstance = nullptr;
    }

    return;
}

void Computer::display()
{
    cout << this->_brand << ":" << this->_price << endl;

    return;
}

Computer::Computer(int price, const char *brand)
    :_price(price)
     ,_brand(new char[strlen(brand) + 1])
{
    strcpy(_brand, brand);
}

Computer::~Computer()
{}
